package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import com.itheima.vo.DishByIdVo;
import com.itheima.vo.DishListVo;
import com.itheima.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    //接口三要素
    //请求方法和地址
    //请求入参 Request
    //请求出参 Response
    @Autowired
    private DishService dishService;
    //请求方法   请求地址
    //RequestBody注解后面一定要实体类

    //添加菜品
    @PostMapping
    public CommonResult addDish(@RequestBody DishDto dishDto){
        boolean res = dishService.addDish(dishDto);
        if (!res){
            return CommonResult.error("添加菜品失败");
        }
        return CommonResult.success("添加菜品成功");
    }


    //菜品分页查询
    @GetMapping("/page")   //name判断了为空的情况，其他为空情况没判断，因此设置一个默认值@RequestParam
    public CommonResult page(String name ,
                             //默认page为1
                             @RequestParam(defaultValue = "1") Integer page ,
                             //默认pageSize为10
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<DishVo> page1 = dishService.page(name,page, pageSize);
        return CommonResult.success(page1);
    }


    //菜品下拉列表
    @GetMapping("/list")
    public CommonResult list(String categoryId){
        List<DishListVo> list = dishService.getList(categoryId);
        return CommonResult.success(list);
    }



    //根据id查询菜品信息
    @GetMapping("/{id}")
    public CommonResult getDishById(@PathVariable String id){

        DishByIdVo dishById = dishService.getDishById(id);

        return CommonResult.success(dishById);
    }


    //修改菜品
    @PutMapping
    public CommonResult updateDish(@RequestBody DishDto dishDto){
        boolean res = dishService.updateDish(dishDto);
        if (!res){
            return CommonResult.error("修改菜品失败");
        }
        return CommonResult.success("修改菜品成功");

        //牛逼写法
//        return new CommonResult(res ? 200 : 500, res ? "修改菜品失败":"修改菜品成功",null);
    }


    //删除菜品
    @DeleteMapping
    public CommonResult deleteDish(@RequestParam List<String> ids){
        boolean res = dishService.deleteDish(ids);
        if (!res){
            return CommonResult.error("删除菜品失败");
        }
        return CommonResult.success("删除菜品成功");
    }


    //菜品停售/起售
    @PostMapping("/status/{status}")
    public CommonResult status(@PathVariable Integer status,  @RequestParam List<String> ids){
        boolean res = dishService.status(status,ids);
        if (!res){
            return CommonResult.error("菜品停售/起售失败");
        }
        return CommonResult.success("菜品停售/起售成功");
    }


}
